#include "MenuScene.h"
#include "SimpleAudioEngine.h"
#include "GameMainScene.h"
#include "cocos2d.h"
#include "GameKey.h"
#include "AboutScene.h"
#include "json/json.h"

USING_NS_CC;

MenuScene::MenuScene()
{
}

MenuScene::~MenuScene()
{
	
}

bool MenuScene::m_isSound = false;

bool  MenuScene::init()
{
	if (!CCLayer::init() )
	{
		return false;
	}
	//CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFrame(CCSpriteFrame::create("bg.png", CCRectMake(0, 0, 640, 960)), ;
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCSprite* back = CCSprite::create("bg.jpg");
	back->setPosition(ccp(size.width/2, size.height/2));
	back->setScaleX(0.5);
	this->addChild(back, 0, ETagTypeBack);

	CCSprite* moon = CCSprite::create("teach_abord1.png");
	moon->setAnchorPoint(ccp(1, 0));
	moon->setPosition(ccp(size.width, 0));
	moon->setScale(0.8);
	this->addChild(moon, 0, ETagTypeMoon);

	CCSprite* meowstar = CCSprite::create("meowstar.png");
	meowstar->setPosition(ccp(size.width/2, size.height - 40));
	meowstar->setScale(0.5);
	this->addChild(meowstar, 0, ETagTypeMeowstar);

	CCSprite* battle = CCSprite::create("battle.png");
	battle->setPosition(ccp(size.width / 2, size.height - 100));
	battle->setScale(0.5);
	this->addChild(battle, 0, ETagTypeBattle);

	CCMenuItemImage* startGameMenuItme = CCMenuItemImage::create("newgameA.png", "newgameB.png", this, menu_selector(MenuScene::menuNewGameCallBack));
	startGameMenuItme->setPosition(ccp(size.width / 2, size.height - 180));
	startGameMenuItme->setScale(0.5);
	startGameMenuItme->setEnabled(false);

	CCMenuItemImage* continueGameMenuItme = CCMenuItemImage::create("continueA.png", "continueB.png", this, menu_selector(MenuScene::menuContinueGameCallBack));
	continueGameMenuItme->setPosition(ccp(size.width / 2, size.height - 230));
	continueGameMenuItme->setScale(0.5);
	continueGameMenuItme->setEnabled(false);

	CCMenuItemImage* aboutGameMenuItme = CCMenuItemImage::create("aboutA.png", "aboutB.png", this, menu_selector(MenuScene::menuAboutGameCallBack));
	aboutGameMenuItme->setPosition(ccp(size.width / 2, size.height - 280));
	aboutGameMenuItme->setScale(0.5);
	aboutGameMenuItme->setEnabled(false);
	
	CCMenuItemImage* closeGameMenuItme = CCMenuItemImage::create("CloseNormal.png", "CloseSelected.png", this, menu_selector(MenuScene::menuCloseGameCallBack));
	//closeGameMenuItme->setScale(0.5);
	closeGameMenuItme->setAnchorPoint(ccp(1, 0));
	closeGameMenuItme->setPosition(ccp(size.width , 0 ));
	closeGameMenuItme->setEnabled(false);

	m_soundMenuItem = CCMenuItemImage::create("sound-on-A.png", "sound-on-B.png", this, menu_selector(MenuScene::menuSoundGameCallBack));
	m_soundMenuItem->setScale(0.5);
	m_soundMenuItem->setAnchorPoint(ccp(0, 0));
	m_soundMenuItem->setPosition(ccp(0, 0));
	m_soundMenuItem->setEnabled(false);

	m_isSound = CCUserDefault::sharedUserDefault()->getBoolForKey(GAME_KEY_SOUND_KEY.c_str(), true );

	if (!m_isSound)
	{
		
		m_soundMenuItem->setNormalImage(CCSprite::create("sound-off-A.png"));
		m_soundMenuItem->setSelectedImage(CCSprite::create("sound-off-B.png"));
	}
	else
	{
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathForFilename("piantou.mp3").c_str());
		CocosDenshion::SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.3);
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathForFilename("piantou.mp3").c_str(), true);
	}
	
	CCMenu* menu = CCMenu::create(startGameMenuItme, continueGameMenuItme, aboutGameMenuItme, closeGameMenuItme, m_soundMenuItem, NULL);
	menu->setPosition(ccp(0, 0));
	this->addChild(menu, 1, ETagTypeMenu);

	return true;

}

CCScene* MenuScene::scene()
{
	CCScene *pScene = CCScene::create();
	CCLayer* pLayer = MenuScene::create();
	pScene->addChild(pLayer);
	return pScene;

}

void MenuScene::onEnter()
{
	CCLayer::onEnter();

	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCSprite* back = (CCSprite*)( this->getChildByTag(ETagTypeBack));
	back->setPosition(ccp(-100, size.height/2 ));
	back->runAction(CCEaseBackOut::create(CCMoveTo::create(0.5, ccp(size.width / 2, size.height / 2))));

	CCSprite* moon = (CCSprite*)(this->getChildByTag(ETagTypeMoon));
	moon->setPosition(ccp(-100, 0));
	moon->runAction(CCEaseBackOut::create(CCMoveTo::create(0.5, ccp(size.width, 0))));

	CCSprite* battle = (CCSprite*)(this->getChildByTag(ETagTypeBattle));
	battle->setPosition(ccp(size.width / 2, size.height + 100 ));
	battle->runAction(CCEaseBackOut::create(CCMoveTo::create(0.5, ccp(size.width / 2, size.height - 100))));


	CCSprite* meowstar = (CCSprite*)(this->getChildByTag(ETagTypeMeowstar));
	meowstar->setPosition(ccp(size.width / 2, size.height + 100));
	meowstar->runAction(CCEaseBackOut::create(CCMoveTo::create(0.5, ccp(size.width / 2, size.height - 40))));

	CCMenu* menu = (CCMenu*)(this->getChildByTag(ETagTypeMenu));
	menu->runAction(CCSequence::create(CCFadeIn::create(1),NULL));
	menu->runAction(CCCallFuncN::create(this, callfuncN_selector(MenuScene::enterMenuCallBack)));




}
void MenuScene::onExit()
{
	CCLayer::onExit();
}


void MenuScene::menuNewGameCallBack(CCObject* pSender)
{
	CCScene* pScene = GameMainScene::scene();
	CCDirector::sharedDirector()->replaceScene(pScene);
}

void MenuScene::menuContinueGameCallBack(CCObject* pSender)
{

}

void MenuScene::menuAboutGameCallBack(CCObject* pSender)
{
	CCScene* scene = AboutScene::scene();
	CCDirector::sharedDirector()->replaceScene(scene);


}

void  MenuScene::menuCloseGameCallBack(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.", "Alert");
#else
	CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
#endif
}
void  MenuScene::menuSoundGameCallBack(CCObject* pSender)
{
	if (!m_soundMenuItem )
	{
		return;
	}
	if (m_isSound)
	{
		m_isSound = false;
		m_soundMenuItem->setNormalImage(CCSprite::create("sound-off-A.png"));
		m_soundMenuItem->setSelectedImage(CCSprite::create("sound-off-B.png"));
		CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();

	}
	else
	{
		m_isSound = true;
		m_soundMenuItem->setNormalImage(CCSprite::create("sound-on-A.png"));
		m_soundMenuItem->setSelectedImage(CCSprite::create("sound-on-B.png"));
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathForFilename("piantou.mp3").c_str(), true);
	}
	CCUserDefault::sharedUserDefault()->setBoolForKey(GAME_KEY_SOUND_KEY.c_str(), m_isSound);
	CCUserDefault::sharedUserDefault()->flush();
}
void MenuScene::enterMenuCallBack(CCNode* pSender)
{
	CCMenu* menu = (CCMenu*)(this->getChildByTag(ETagTypeMenu));
	if (menu)
	{
		CCArray* children = menu->getChildren();
		for (unsigned int i = 0; i < children->count(); i++)
		{
			CCMenuItemImage* image = (CCMenuItemImage*)(children->objectAtIndex(i));
			if (image)
			{
				image->setEnabled(true);
			}
		}
	}

}